<template>
  <el-card>
    <div class="header">
      <div class="header_left">
        <!-- 顶部搜索框和按钮 -->
        <el-input
          clearable
          v-model="pageInfo.qurey"
          prefix-icon="el-icon-search"
          placeholder="请输入搜索内容"
          style="width: 400px"
          @clear="getRateList"
        >
          <el-button slot="append" icon="el-icon-search" @click="getRateList"></el-button>
        </el-input>
      </div>
      <div class="header_right">
        <el-button type="primary" @click="back">返回</el-button>
      </div>
    </div>
    <!-- 表格数据区域 -->
    <el-table
      :data="tableData"
      stripe
      style="width: 100%"
      border
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column prop="userName" label="评价用户" width="230">
        <template slot-scope="scope">
          <span v-if="scope.row.isHide == 0">{{ scope.row.userName }}</span>
          <span v-if="scope.row.isHide == 1">匿名用户</span>
        </template>
      </el-table-column>
      <el-table-column prop="rateText" label="评价内容" width="530">
        <template slot-scope="scope">
          <div class="text">{{ scope.row.rateText }}</div>
          <div class="img" v-if="scope.row.rateImgList.length != 0">
            <el-image :src="getImage(item.rateImage)" fit="cover" v-for="(item, index) in scope.row.rateImgList" :key="index">
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline"></i>
              </div>
            </el-image>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="rateTime" label="评价时间" width="180"></el-table-column>
      <el-table-column prop="rateScore" label="评分" width="150"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <!-- 亲，非常抱歉因为我们的疏忽给你带来一次不愉快的用餐体验，感谢亲的宝贵意见，我们后期会加以改进和完善，祝你生活愉快。 -->
          <el-button type="text" class="edit" @click="edit(scope.row)" v-if="scope.row.rateReturn == '' || scope.row.rateReturn == null"
            >回复</el-button
          >
          <span class="line" v-if="scope.row.rateReturn == '' || scope.row.rateReturn == null">|</span>
          <el-button type="text" class="delete" @click="deleteRate(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页导航栏区域 -->
    <div class="page">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.pageNum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 编辑商家对话框 -->
    <el-dialog title="回复评价" :visible.sync="rateReturnDialog" width="40%" @close="editRateDiologClose">
      <el-form ref="editRateRef" :model="editRateForm" label-width="60px" :rules="addRateRules">
        <el-form-item label="回复" prop="rateReturn">
          <el-input type="textarea" v-model="editRateForm.rateReturn" clearable placeholder="请输入回复内容"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="rateReturnDialog = false">取 消</el-button>
        <el-button type="primary" @click="editRate">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      shopId: '',
      //分页查询数据
      pageInfo: {
        //模糊查询的数据
        qurey: '',
        //当前页数
        pageNum: 1,
        //每页显示的数据条数
        pageSize: 5,
        shopId: '',
      },
      //表格数据
      tableData: [],
      //数据总条数
      total: 0,
      rateReturnDialog: false,
      editRateForm: [],
      //添加商家表单验证规则
      addRateRules: {
        rateReturn: [{ required: true, message: '请输入回复内容', trigger: 'blur' }],
      },
    };
  },

  created() {
    this.shopId = this.$route.query.shopId;
    this.getRateList();
  },

  methods: {
    back() {
      this.$router.go(-1);
    },
    async deleteRate(rateId) {
      const { data: res } = await this.$http.delete('rate/deleteRate/' + rateId);
      if (res.code == 1) {
        this.getRateList();
        return this.$message.success(res.data);
      } else {
        return this.$message.error('删除失败');
      }
    },
    editRate() {
      this.rateReturnDialog = false;
      this.$refs.editRateRef.validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.$http.put('rate/updateRate', this.editRateForm);
          if (res.code == 1) {
            this.getRateList();
            return this.$message.success(res.data);
          } else {
            return this.$message.error('回复提交失败');
          }
        } else {
          return this.$message.error('表单校验失败');
        }
      });
    },
    edit(rateInfo) {
      this.rateReturnDialog = true;
      this.editRateForm = rateInfo;
    },
    editRateDiologClose() {
      this.$refs.editRateRef.resetFields();
    },
    handleSizeChange(newSize) {
      this.pageInfo.pageSize = newSize;
      this.getShopList();
    },

    handleCurrentChange(newPageNum) {
      this.pageInfo.pageNum = newPageNum;
      this.getShopList();
    },
    //图片的下载
    getImage(image) {
      return image ? `http://localhost:8080/school/image/download?name=${image}` : '';
    },
    async getRateList() {
      this.pageInfo.shopId = this.shopId;
      const { data: res } = await this.$http.get('rate/getPage', { params: this.pageInfo });
      if (res.code == 1) {
        this.tableData = res.data.records;
        this.total = res.data.total;
      } else {
        return this.$message.error('评价信息获取失败');
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.image-slot {
  .el-icon-picture-outline {
    font-size: 55px;
    line-height: 100px;
    text-align: center;
  }
}
.img {
  :deep(.el-image) {
    height: 50px;
    width: 50px;
    border-radius: 10px;
  }
}
.el-table {
  margin-top: 20px;
}
.edit {
  color: #ffc200;
  font-size: 13px;
}
.delete {
  color: #f56c6c;
  font-size: 13px;
}
.line {
  color: #ebeef5;
  font-size: 13px;
  margin: 0 5px 0 5px;
}
.page {
  display: flex;
  justify-content: space-evenly;
  margin-top: 25px;
}
</style>